const app = getApp();
import { label_type, preservationUser_label } from "../../../api/homePage"
import { getStorage } from "../../../utils/localStorage"
import { baseUrl } from "../../../api/request"
Page({
    data: {
        user: getStorage("user"),
        baseUrl: baseUrl,
        navData: {
            level: 2,
            title: '话题'
        },
        labelList: [], //全部标签列表
        labelArr: [], //分类下的标签
        leftIndex: 0, //左边导航索引
        type: null, //页面入口类型
        dynamicId: null, //动态id
        choiceArr: [], //选择的标签
        classifyId: [], //分类标签id
        navMargin: app.globalData.navHeight + app.globalData.navTop,
    },
    onLoad(options) {
        if (JSON.stringify(options) != "{}") {
            this.setData({
                type: options.type,
                dynamicId: Number(options.dynamicId) || "",
            });
        };
        this.getLabelTypeList();
        this.setData({
            user: getStorage("user"),
            baseUrl: baseUrl,
        });
    },
    onReady() {

    },
    onShow() {

    },
    //获取标签分类列表
    getLabelTypeList() {
        label_type({ dynamicId: this.data.dynamicId }).then(res => {
            this.setData({
                labelList: res.data.data,
                labelArr: res.data.data[0].labels,
                classifyId: res.data.data[0].id
            });
            console.log(this.data.labelList, 'labelList');
        })
    },
    //左边导航栏点击事件
    leftnavClick(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index;
        this.setData({
            leftIndex: index,
            labelArr: item.labels,
            classifyId: item.id
        });
    },

    //checkbox Change事件
    xzChange(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index,
            value = "labelList[" + this.data.leftIndex + "].labels[" + index + "].flagLabel",
            arr = this.data.choiceArr;
        if (item.flagLabel) {
            arr.splice(index, 1);
        } else {
            arr.push(item);
        };
        this.setData({
            [value]: item.flagLabel ? false : true,
            choiceArr: arr
        });
    },
    //确认话题按钮提交
    sunbmit() {
        if (this.data.choiceArr.length === 0) {
            wx.showToast({
                icon: 'none',
                title: '请选择话题',
            })
        } else {
            if (this.data.type === "发布") {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2]; //上一个页面
                prevPage.setData({
                    data: this.data.choiceArr
                })
                wx.navigateBack({//返回
                    delta: 1
                });
            } else {
                preservationUser_label({ labelId: this.data.label, userId: this.data.user.id }).then(res => { }).then(res => {
                    wx.showToast({
                        icon: 'none',
                        title: res.data.msg,
                    })
                });
                wx.navigateBack();
            }
        };

    },
    onHide() {

    },
    onUnload() {

    },
    onPullDownRefresh() {

    },
    //页面触底
    onReachBottom() {

    },
    //用户点击右上角分享
    onShareAppMessage() {

    }
})